package com.employeemanagementsystem.dao;

import com.employeemanagementsystem.model.Feedback;
import com.employeemanagementsystem.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FeedbackDao {

    public boolean addFeedback(Feedback feedback) {
        String sql = "INSERT INTO feedback(employee_id, content, status, created_at, updated_at) VALUES (?, ?, 'PENDING', NOW(), NOW())";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, feedback.getEmployeeId());
            stmt.setString(2, feedback.getContent());

            int rowsAffected = stmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }


    public List<Feedback> getPendingFeedbacks() {
        List<Feedback> feedbackList = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM feedback WHERE status = 'PENDING'")) {

            while (rs.next()) {
                Feedback feedback = new Feedback();
                feedback.setId(rs.getInt("id"));
                feedback.setEmployeeId(rs.getInt("employee_id"));
                feedback.setContent(rs.getString("content"));
                feedback.setStatus(rs.getString("status"));
                feedback.setCreatedAt(rs.getString("created_at"));
                feedback.setUpdatedAt(rs.getString("updated_at"));
                feedback.setResponse(rs.getString("response"));

                feedbackList.add(feedback);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return feedbackList;
    }

    public List<Feedback> getFeedbacksByEmployeeId(int employeeId) {
        List<Feedback> feedbackList = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement("SELECT * FROM feedback WHERE employee_id = ?")) {
            stmt.setInt(1, employeeId);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                Feedback feedback = new Feedback();
                feedback.setId(rs.getInt("id"));
                feedback.setEmployeeId(rs.getInt("employee_id"));
                feedback.setContent(rs.getString("content"));
                feedback.setStatus(rs.getString("status"));
                feedback.setCreatedAt(rs.getString("created_at"));
                feedback.setUpdatedAt(rs.getString("updated_at"));
                feedback.setResponse(rs.getString("response"));

                feedbackList.add(feedback);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return feedbackList;
    }

    public List<Feedback> getProcessedFeedbacks() {
        List<Feedback> feedbackList = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM feedback WHERE status = 'PROCESSED'")) {

            while (rs.next()) {
                Feedback feedback = new Feedback();
                feedback.setId(rs.getInt("id"));
                feedback.setEmployeeId(rs.getInt("employee_id"));
                feedback.setContent(rs.getString("content"));
                feedback.setStatus(rs.getString("status"));
                feedback.setCreatedAt(rs.getString("created_at"));
                feedback.setUpdatedAt(rs.getString("updated_at"));
                feedback.setResponse(rs.getString("response"));

                feedbackList.add(feedback);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return feedbackList;
    }


    public boolean updateFeedbackStatus(int feedbackId, String response) {
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(
                     "UPDATE feedback SET status = 'PROCESSED', response = ?, updated_at = NOW() WHERE id = ?")) {
            stmt.setString(1, response);
            stmt.setInt(2, feedbackId);

            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}



